package cn.edu.cug.cs.gtl.docsrv.doc;

import cn.edu.cug.cs.gtl.mybatis.Session;
import cn.edu.cug.cs.gtl.mybatis.mapper.doc.*;
import cn.edu.cug.cs.gtl.mybatis.metadata.DocUtils;
import cn.edu.cug.cs.gtl.protos.TimeValue;
import cn.edu.cug.cs.gtl.protoswrapper.TimeValueWrapper;

import javax.print.Doc;
import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 文档数据管理器
 */
public class DocManager {
    protected Session session;//数据库会话

    /**
     * 构造函数
     * @param s
     */
    public DocManager(Session s){
        this.session=s;
    }

    /**
     * 获取数据库会话
     * @return
     */
    public Session getSession(){
        return this.session;
    }

    /**
     * 设置数据库会话
     * @param s
     */
    public void setSession(Session s){
        this.session=s;
    }
    /**
     * 将一个文档插入数据库
     * @param docItem
     * @param docFile
     * @return
     */
    public boolean insert(DocItem docItem, DocFile docFile){
        DocItemMapper docItemMapper = session.getMapper(DocItemMapper.class);
        if(docItem.getDocInternalId()==null){
            docItem.setDocInternalId(docItemMapper.nextDocInternalId());
        }
        docItemMapper.insert(docItem);
        DocFileMapper docFileMapper = session.getMapper(DocFileMapper.class);
        docFileMapper.insert(docFile);
        session.commit();
        return true;
    }

    /**
     *
     * @param docAll
     * @return
     */
    public  boolean insert(DocAll docAll){
        DocAllMapper docAllMapper = session.getMapper(DocAllMapper.class);
        if(docAll.getDocInternalId()==null) {
            docAll.setDocInternalId(docAllMapper.nextDocInternalId());
        }
        docAllMapper.insert(docAll);
        session.commit();
        return true;
    }

    /**
     * 根据docInternalId删除文档记录
     * @param docItem 必须填充docInternalId
     * @return
     */
    public boolean delete(DocItem docItem){
        BigDecimal docInternalId = docItem.getDocInternalId();
        DocItemMapper docItemMapper = session.getMapper(DocItemMapper.class);
        docItemMapper.deleteByPrimaryKey(docInternalId);
        DocFileMapper docFileMapper = session.getMapper(DocFileMapper.class);
        docFileMapper.deleteByPrimaryKey(docInternalId);
        session.commit();
        return true;
    }

    /**
     * 根据docInternalId删除文档记录
     * @param docAll 必须填充docInternalId
     * @return
     */
    public boolean delete(DocAll docAll){
        BigDecimal docInternalId = docAll.getDocInternalId();
        DocAllMapper docAllMapper = session.getMapper(DocAllMapper.class);
        docAllMapper.deleteByPrimaryKey(docInternalId);
        session.commit();
        return true;
    }

    /**
     * 根据docInternalId更新数据库中的记录
     * @param docItem 该对象的docInternalId必须填充
     * @param docFile 更新文档文件的内容
     * @return 更新成功返回true,否则返回false
     */
    public boolean update(DocItem docItem, DocFile docFile){
        DocItemMapper docItemMapper = session.getMapper(DocItemMapper.class);
        docItemMapper.updateByPrimaryKey(docItem);
        DocFileMapper docFileMapper = session.getMapper(DocFileMapper.class);
        docFileMapper.updateByPrimaryKey(docFile);
        session.commit();
        return true;
    }

    /**
     * 根据docInternalId更新数据库中的记录
     * @param docAll 该对象的docInternalId必须填充
     * @return 更新成功返回true,否则返回false
     */
    public boolean update(DocAll docAll){
        DocAllMapper docAllMapper = session.getMapper(DocAllMapper.class);
        docAllMapper.updateByPrimaryKey(docAll);
        session.commit();
        return true;
    }

    /**
     * 根据docInternalId查询文档 ,默认查询不带file 二进制文件
     * @param docInternalId
     * @return
     */
    public  DocAll queryByInternalId(BigDecimal  docInternalId){
        DocItemMapper docItemMapper = session.getMapper(DocItemMapper.class);
        DocItem docItem = docItemMapper.selectByPrimaryKey(docInternalId);
        DocFile docFile = new DocFile();
        // file 为空
        docFile.setDocRawBytes(null);
        DocAll docAll = DocUtils.toDocAll(docItem,docFile);
        return docAll;
    }

    /**
     * 根据文档标题查询文档
     * @param docEnTitle
     * @return
     */
    public  List<DocAll> queryByTitle(String docEnTitle){
        List<DocAll> res = new ArrayList<>();
        DocItemMapper docItemMapper = session.getMapper(DocItemMapper.class);
        List<DocItem> docItems = docItemMapper.selectByEnTitle(docEnTitle);
        for (DocItem docItem : docItems){
            DocFile docFile = new DocFile();
            // file 为空
            docFile.setDocRawBytes(null);
            DocAll docAll = DocUtils.toDocAll(docItem,docFile);
            res.add(docAll);
        }
        return res;

    }

    /**
     * 根据文档注释查询文档
     * @param docCnTitle  文档注释
     * @return
     */
    public  List<DocAll> queryByComment(String docCnTitle){
        List<DocAll> res = new ArrayList<>();
        DocItemMapper docItemMapper = session.getMapper(DocItemMapper.class);
        List<DocItem> docItems = docItemMapper.selectByCnTitle(docCnTitle);
        for (DocItem docItem : docItems){
            DocFile docFile = new DocFile();
            // file 为空
            docFile.setDocRawBytes(null);
            DocAll docAll = DocUtils.toDocAll(docItem,docFile);
            res.add(docAll);
        }
        return res;
    }


    /**
     * 根据文档类型查询文档
     * @param docType
     * @return
     */
    public  List<DocAll> queryByType(String docType){
        List<DocAll> res = new ArrayList<>();
        DocItemMapper docItemMapper = session.getMapper(DocItemMapper.class);
        List<DocItem> docItems = docItemMapper.selectByType(docType);
        for (DocItem docItem : docItems){
            DocFile docFile = new DocFile();
            // file 为空
            docFile.setDocRawBytes(null);
            DocAll docAll = DocUtils.toDocAll(docItem,docFile);
            res.add(docAll);
        }
        return res;
    }

    /**
     * 从文档库中提取文档添加时间段为[begin,end)之内的文档
     * @param begin
     * @param end
     * @return
     */
    public  List<DocAll> queryByAppendDate(TimeValue begin, TimeValue end){
        List<DocAll> res = new ArrayList<>();
        DocItemMapper docItemMapper = session.getMapper(DocItemMapper.class);
        List<DocItem> docItems = docItemMapper.selectByAppendDate(TimeValueWrapper.toUtilDate(begin),TimeValueWrapper.toUtilDate(end));
        for (DocItem docItem : docItems){
            DocFile docFile = new DocFile();
            // file 为空
            docFile.setDocRawBytes(null);
            DocAll docAll = DocUtils.toDocAll(docItem,docFile);
            res.add(docAll);
        }
        return res;
    }





    public List<DocAll> queryByAppendDate(LocalDateTime begin, LocalDateTime end){
        return queryByAppendDate(TimeValueWrapper.from(begin),TimeValueWrapper.from(end));
    }
    public List<DocAll> queryByAppendDate(LocalDate begin, LocalDate end){
        return queryByAppendDate(TimeValueWrapper.from(begin),TimeValueWrapper.from(end));
    }
    public List<DocAll> queryByAppendDate(Timestamp begin, Timestamp end){
        return queryByAppendDate(TimeValueWrapper.from(begin),TimeValueWrapper.from(end));
    }

    /**
     * 将本地文件插入文档库中
     * @param docItem 该文档的元数据
     * @param localSingleFile 文档文件全名
     * @return
     */
    public  boolean insert(DocItem docItem, String localSingleFile){
        return false;
    }

    /**
     * 将本地目录下的所有文件插入到文档库中
     * @param localDir 本地目录
     * @return
     */
    public  boolean insert(String localDir){
        return false;
    }


}
